﻿<%@ WebHandler Language="C#" Class="MachineTaskPlanReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

/**
 * Description: 设备排产查询报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2022.4.22
 **/

public class MachineTaskPlanReport : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getSearchSqlString(JObject jObject)
    {
        string factoryid = "", workcenterid = "", teamid = "";
        JArray selectOrg = JArray.Parse(jObject["selectOrg"].ToString());
        if (selectOrg.Count == 3)
        {
            factoryid = selectOrg[0].ToString();
            workcenterid = selectOrg[1].ToString();
            teamid = selectOrg[2].ToString();
        }
        else if (selectOrg.Count == 2)
        {
            factoryid = selectOrg[0].ToString();
            workcenterid = selectOrg[1].ToString();
        }
        else
        {
            factoryid = selectOrg[0].ToString();
        }
        string sql = @"
SELECT rd.description,rd.resourcename,cci.mfgordername,cci.productname,cci.productdescription,cci.togetherflag,
       cci.containername,csr.workflowstepname,csr.workcentername,csr.teamname,csr.dispatchemployeename,
       to_char(csd.plannedstartdate,'yyyy/mm/dd') plannedstartdate,
       to_char(csd.plannedfinishdate,'yyyy/mm/dd') plannedfinishdate,
       e.fullname recvPerson,
       csd.recvdate,csr.reportdate,
       CASE WHEN TRUNC(NVL(csr.reportdate,SYSDATE)) - TRUNC(csd.plannedfinishdate) < 0 THEN 0
            ELSE TRUNC(NVL(csr.reportdate,SYSDATE)) - TRUNC(csd.plannedfinishdate) END dalayDays
FROM containersteprecord csr
LEFT JOIN containercurrentinfo cci ON cci.containerid = csr.containerid
LEFT JOIN resourcedef rd ON rd.resourceid = csr.dispatchresourceid
LEFT JOIN containerstepdispatch csd ON csd.containerstepdispatchid = csr.dispatchid AND csd.isuse = 0
LEFT JOIN employee e ON e.employeeid = csd.recvemployeeid
WHERE rd.description IS NOT NULL AND rd.description <> '无'";
        // 分厂
        if (factoryid != "")
        {
            sql += string.Format(" AND cci.originalfactoryid = '{0}'", factoryid);
        }

        // 工区
        if (workcenterid != "")
        {
            sql += string.Format(" AND csr.workcenterid = '{0}'", workcenterid);
        }

        // 班组
        if (teamid != "")
        {
            sql += string.Format(" AND csr.teamid = '{0}'", teamid);
        }

        //订单号
        if (jObject["mfgordername"] != null && jObject["mfgordername"].ToString() != "")
        {
            sql += string.Format(" AND cci.mfgordername = '{0}'", jObject["mfgordername"].ToString());
        }

        //零件图号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND cci.productname = '{0}'", jObject["productname"].ToString());
        }

        //流水卡号
        if (jObject["containername"] != null && jObject["containername"].ToString() != "")
        {
            sql += string.Format(" AND cci.containername = '{0}'", jObject["containername"].ToString());
        }

        //设备编号
        if (jObject["machineno"] != null && jObject["machineno"].ToString() != "")
        {
            sql += string.Format(" AND rd.resourcename LIKE '%{0}%'", jObject["machineno"].ToString());
        }

        //派工开始时间
        if (jObject["plannedstartdate"] != null && jObject["plannedstartdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["plannedstartdate"].ToString());
            sql += string.Format(" AND csd.plannedstartdate >= TO_DATE('{0}','yyyy-mm-dd')", arr[0].ToString());
            sql += string.Format(" AND csd.plannedstartdate <= TO_DATE('{0}','yyyy-mm-dd')", arr[1].ToString());
        }

        //派工完成时间
        if (jObject["plannedfinishdate"] != null && jObject["plannedfinishdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["plannedfinishdate"].ToString());
            sql += string.Format(" AND csd.plannedfinishdate >= TO_DATE('{0}','yyyy-mm-dd')", arr[0].ToString());
            sql += string.Format(" AND csd.plannedfinishdate <= TO_DATE('{0}','yyyy-mm-dd')", arr[1].ToString());
        }

        //派工人
        if (jObject["dispatcher"] != null && jObject["dispatcher"].ToString() != "")
        {
            sql += string.Format(" AND csr.dispatchemployeename LIKE '%{0}%'", jObject["dispatcher"].ToString());
        }

        //领工时间
        if (jObject["linggongdate"] != null && jObject["linggongdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["linggongdate"].ToString());
            sql += string.Format(" AND csd.recvdate >= TO_DATE('{0} 00:00:00','yyyy-mm-dd hh24:mi:ss')", arr[0].ToString());
            sql += string.Format(" AND csd.recvdate <= TO_DATE('{0} 23:59:59','yyyy-mm-dd hh24:mi:ss')", arr[1].ToString());
        }

        //报工时间
        if (jObject["reportdate"] != null && jObject["reportdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["reportdate"].ToString());
            sql += string.Format(" AND csr.reportdate >= TO_DATE('{0} 00:00:00','yyyy-mm-dd hh24:mi:ss')", arr[0].ToString());
            sql += string.Format(" AND csr.reportdate <= TO_DATE('{0} 23:59:59','yyyy-mm-dd hh24:mi:ss')", arr[1].ToString());
        }

        //领工人
        if (jObject["ligongren"] != null && jObject["ligongren"].ToString() != "")
        {
            sql += string.Format(" AND e.fullname LIKE '%{0}%'", jObject["ligongren"].ToString());
        }
        return sql;
    }

    //查询方法
    Result getData(JObject jObject)
    {
        Result r = new Result();
        string sql = getSearchSqlString(jObject);
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dt = dto.DBTable;
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = getSearchSqlString(jObject);
        DataTable dt = dal.GetDataTable(sql);
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        dt.Columns["description"].Caption = "设备名称";
        dt.Columns["resourcename"].Caption = "设备编号";
        dt.Columns["mfgordername"].Caption = "订单号";
        dt.Columns["productname"].Caption = "零件号";
        dt.Columns["productdescription"].Caption = "零件名称";
        dt.Columns["togetherflag"].Caption = "批次号";
        dt.Columns["containername"].Caption = "流水卡号";
        dt.Columns["workflowstepname"].Caption = "工序";
        dt.Columns["workcentername"].Caption = "工区";
        dt.Columns["teamname"].Caption = "班组";
        dt.Columns["dispatchemployeename"].Caption = "派工人";
        dt.Columns["plannedstartdate"].Caption = "计划开始时间";
        dt.Columns["plannedfinishdate"].Caption = "计划完成时间";
        dt.Columns["recvPerson"].Caption = "领工人";
        dt.Columns["recvdate"].Caption = "领工时间";
        dt.Columns["reportdate"].Caption = "报工时间";
        dt.Columns["dalayDays"].Caption = "超期天数";
        ExportHelper.ExportExcel(dt, "设备排产查询报表");
    }
}